package com.ruoyi.wms.service;

import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.core.utils.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.wms.domain.bo.employee.EmployeeTrainingBo;
import com.ruoyi.wms.domain.vo.EmployeeTrainingVo;
import com.ruoyi.wms.domain.entity.employee.EmployeeTraining;
import com.ruoyi.wms.mapper.EmployeeTrainingMapper;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 员工培训Service业务层处理
 *
 * @author 邓滔滔
 * @date 2024-12-04
 */
@RequiredArgsConstructor
@Service
public class EmployeeTrainingService {

    private final EmployeeTrainingMapper employeeTrainingMapper;

    /**
     * 查询员工培训
     */
    public EmployeeTrainingVo queryById(Long id){
        return employeeTrainingMapper.selectVoById(id);
    }

    /**
     * 查询员工培训列表
     */
    public TableDataInfo<EmployeeTrainingVo> queryPageList(EmployeeTrainingBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EmployeeTraining> lqw = buildQueryWrapper(bo);
        Page<EmployeeTrainingVo> result = employeeTrainingMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询员工培训列表
     */
    public List<EmployeeTrainingVo> queryList(EmployeeTrainingBo bo) {
        LambdaQueryWrapper<EmployeeTraining> lqw = buildQueryWrapper(bo);
        return employeeTrainingMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EmployeeTraining> buildQueryWrapper(EmployeeTrainingBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EmployeeTraining> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), EmployeeTraining::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getTrainingType()), EmployeeTraining::getTrainingType, bo.getTrainingType());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            EmployeeTraining::getCreateTime ,params.get("beginCreateTime"), params.get("endCreateTime"));
        return lqw;
    }

    /**
     * 新增员工培训
     */
    public void insertByBo(EmployeeTrainingBo bo) {
        EmployeeTraining add = MapstructUtils.convert(bo, EmployeeTraining.class);
        employeeTrainingMapper.insert(add);
    }

    /**
     * 修改员工培训
     */
    public void updateByBo(EmployeeTrainingBo bo) {
        EmployeeTraining update = MapstructUtils.convert(bo, EmployeeTraining.class);
        employeeTrainingMapper.updateById(update);
    }

    /**
     * 批量删除员工培训
     */
    public void deleteByIds(Collection<Long> ids) {
        employeeTrainingMapper.deleteBatchIds(ids);
    }
}
